/**
 * 
 */
package fr.miage.client.presentation.menu;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

import fr.miage.client.metier.categorie.CategorieBusinessService;
import fr.miage.client.metier.categorie.CategorieBusinessServiceAsync;
import fr.miage.client.metier.entite.Categorie;


/**
 * @author romain
 *
 */
public class MonTree {

	ServiceDefTarget endpoint;

	CategorieBusinessServiceAsync categorieService;
	
	Tree staticTree;

  public Widget onInitialize() {
  	// création du proxy 
  	categorieService = (CategorieBusinessServiceAsync) GWT.create(CategorieBusinessService.class);
  	endpoint = (ServiceDefTarget) categorieService;
  	String moduleRelativeURL = GWT.getModuleBaseURL() + "categorie";
  	endpoint.setServiceEntryPoint(moduleRelativeURL);
  	
    staticTree =  new Tree();
  	AsyncCallback callback = new AsyncCallback(){
  		public void onSuccess(Object result) {  		
  			List<Categorie> laListe = (List<Categorie>) result; 
  			 List<String> categorieNiv2 = new ArrayList<String>();
  			 System.out.println("La taille dans la liste de Onsuccess"+laListe.size());
  			for (int i = 0;i<laListe.size();i++){
  				//categorieNiv2[i] = laListe.get(i).getNomCate();
  				categorieNiv2.add(laListe.get(i).getNomCate());
  				 System.out.println(laListe.get(i).getNomCate());
  			}
  			
  			System.out.println("La taille dans la liste de Onsuccess2"+laListe.size());
  			System.out.println("La taille dans la String[]"+categorieNiv2.size());
  	    String concertosLabel = "Label 1";
  	    String quartetsLabel ="Label 2";
  	    String sonatasLabel = "Label 3";
  	    String symphoniesLabel = "Label 4";
  	  
  	    
  	    // Add some of Beethoven's music
  	    int i =0;
  	    TreeItem beethovenItem = staticTree.addItem(categorieNiv2.get(i));
  	    i++;
  	    addItemCategorie(beethovenItem, concertosLabel,
  	    		categorieNiv2);
  	    addItemCategorie(beethovenItem, quartetsLabel,
  	    		categorieNiv2);
  	    addItemCategorie(beethovenItem, sonatasLabel,
  	    		categorieNiv2);
  	    addItemCategorie(beethovenItem, symphoniesLabel,
  	    		categorieNiv2);

  	    // Add some of Brahms's music
  	    TreeItem brahmsItem = staticTree.addItem(categorieNiv2.get(i));
  	    i++;
  	    addItemCategorie(brahmsItem, concertosLabel,
  	    		categorieNiv2);
  	    addItemCategorie(brahmsItem, quartetsLabel,
  	    		categorieNiv2);
  	    addItemCategorie(brahmsItem, sonatasLabel,
  	    		categorieNiv2);
  	    addItemCategorie(brahmsItem, symphoniesLabel,
  	    		categorieNiv2);

  	    // Add some of Mozart's music
  	    TreeItem mozartItem = staticTree.addItem(categorieNiv2.get(i));
  	    i++;
  	    addItemCategorie(mozartItem, concertosLabel,
  	    		categorieNiv2);
  	  
  		}
  		
  		public void onFailure(Throwable caught) {
  			 System.out.println("callback tree menu fail");
  		}
  		};
  		
		categorieService.getAllCategorie(callback);
    staticTree.setAnimationEnabled(true);
    staticTree.ensureDebugId("cwTree-staticTree");
    staticTree.setSize("100%", "100%");
    ScrollPanel staticTreeWrapper = new ScrollPanel(staticTree);
    staticTreeWrapper.ensureDebugId("cwTree-staticTree-Wrapper");
    staticTreeWrapper.setSize("200px", "100%");

    // Wrap the static tree in a DecoratorPanel
    DecoratorPanel staticDecorator = new DecoratorPanel();
    staticDecorator.setWidget(staticTreeWrapper);

    // Combine trees onto the page
    Grid grid = new Grid(2, 3);
    grid.setCellPadding(2);
    grid.getRowFormatter().setVerticalAlign(1, HasVerticalAlignment.ALIGN_TOP);
    grid.setWidget(0, 0, staticDecorator);

    return grid;
  }

  /**
   * Add a new section of music created by a specific composer.
   * 
   * @param parent the parent {@link TreeItem} where the section will be added
   * @param label the label of the new section of music
   * @param composerWorks an array of works created by the composer
   */
  private void addItemCategorie(TreeItem parent, String label,
      List<String> composerWorks) {
    TreeItem section = parent.addItem(label);
    for (String work : composerWorks) {
      section.addItem(work);
    }
  }

  /**
   * Create a static tree with some data in it.
   * 
   * @return the new tree
   */
  private Tree createStaticTree() {

    Tree staticTree = new Tree();
  	
  

  	  return staticTree;
  
  }

	/** Constructeur
	 * 
	 */
	public MonTree() {
		super();
	}

}
